package com.kgc.warehouse.bussness.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kgc.warehouse.bussness.dto.CustomerDTO;
import com.kgc.warehouse.bussness.entity.Customer;
import com.kgc.warehouse.bussness.service.CustomerService;
import com.kgc.warehouse.system.common.CodeMsg;
import com.kgc.warehouse.system.common.ResponseResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("customer")
public class CustomerController {

    @Resource
    private CustomerService customerService;

    @RequestMapping("loadAllCustomerForSelect")
    public ResponseResult loadAllCustomerForSelect(){
        LambdaQueryWrapper<Customer> customerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        customerLambdaQueryWrapper.eq(Customer::getAvailable,1);
        List<Customer> list = customerService.list(customerLambdaQueryWrapper);
        return new ResponseResult(list,null, CodeMsg.SUCCESS);
    }

    //分页查询加搜索功能

    @RequestMapping("loadAllCustomer")
    public ResponseResult selectPage(CustomerDTO customerDTO) {
        System.out.println("customerDTO = " + customerDTO);

        return customerService.selectPage(customerDTO);
    }

    @RequestMapping("addCustomer")
    public ResponseResult addCustomer(Customer customer){
        boolean save = customerService.save(customer);
        return new ResponseResult(CodeMsg.ADD_SUCCESS);
    }
    @RequestMapping("updateCustomer")
    public ResponseResult updateCustomer(Customer customer){
        try {
            customerService.updateById(customer);
            return new ResponseResult(CodeMsg.UPDATE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new  ResponseResult(CodeMsg.UPDATE_FAIL);
        }
    }
    @RequestMapping("deleteCustomer")
    public ResponseResult deleteUser(Integer id){
        customerService.removeById(id);
        return new ResponseResult(CodeMsg.DELETE_SUCCESS);
    }

}
